<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>添加请假类型</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form label-width="120px" ref="editForm">
          <el-form-item label="请假类型名称：" prop="num">
            <el-input v-model="name" placeholder="请假类型名称" size="small"></el-input>
          </el-form-item>
          <el-form-item label="请假计时：" prop="num">
            <el-radio-group v-model="time_type">
              <el-radio :label="1">天</el-radio>
              <el-radio :label="2">小时</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="审批人：" prop="num">
            <el-button @click="closeUserFn()" size="small">选择审批人</el-button>
            <div>
              <el-tag
                  closable
                  :disable-transitions="false"
                  @close="handleCloseExamine(index)"
                  class="mr-10"
                  v-for="(items,index) in dynamicTags"
                  :key="items.id"
              >{{ items.realname }}
              </el-tag>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click.native="closePOpup" size="small">取消</el-button>
        <el-button size="small" type="primary" @click="checkEditFn">确定并提交</el-button>
      </div>
    </div>
    <MultipleTeacherModle :visible.sync="userVisible" @studentFn="studentFn"
                          :dynamicTagsArray="dynamicTagsArray"></MultipleTeacherModle>
  </div>
</template>

<script>
import {typeAdd, typeDetail, typeEdit} from '@/api/admin/staffService'

export default {
  props: ['visible', 'id'], // recruitmentType examine 审核
  components: {},
  data() {
    return {
      radio: 1,
      dialogVisible: this.visible,
      userVisible: false,
      dynamicTagsArray: {},
      dynamicTags: [],
      time_type: 1,
      name: '',
      pickerOptions1: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7
        }
      },
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (!this.visible) {
        this.name = ''
        this.dynamicTags = []
        this.dynamicTagsArray = {}
        this.time_type = 1
      }
      if (this.id && this.visible) {
        this.getDetail()
      }
    }
  },
  created() {

  },
  methods: {
    checkEditFn() {
      if (!this.name) {
        this.$message.error('请填写请假类型名称')
        return
      }
      if (!this.dynamicTags.length) {
        this.$message.error('请选择该项审核人')
        return
      }
      let data_ = {
        name: this.name,
        time_type: this.time_type
      }
      let a_ = []
      for (let item of this.dynamicTags) {
        a_.push(item.id)
      }
      data_.user_ids = a_.join(',')
      if (!this.id) {
        typeAdd(data_).then(res => {
          console.log(res)
          if (!res.code) {
            this.$message.success('添加成功')
            this.closePOpup()
            this.$emit('InitFn')
          } else {
            this.$message.error(res.msg)
          }
        })
      } else {
        data_.id = this.id
        typeEdit(data_).then(res => {
          console.log(res)
          if (!res.code) {
            this.$message.success('编辑成功')
            this.closePOpup()
            this.$emit('InitFn')
          } else {
            this.$message.error(res.msg)
          }
        })
      }
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    getDetail() {
      typeDetail({id: this.id}).then(res => {
        console.log(res)
        this.dynamicTags = res.vo.user_ids
        this.name = res.vo.name
        this.time_type = res.vo.time_type
      })
    },
    studentFn(option) {
      console.log('option:', option)
      let that = this
      this.dynamicTags = []
      for (let index in option) {
        let item = option[index]
        this.dynamicTags.push(that.getJson(item))
      }
    },
    closeUserFn() {
      let list = {}
      for (let item of this.dynamicTags) {
        list[item.id] = item
      }
      this.dynamicTagsArray = list
      this.userVisible = true
    },
    handleCloseExamine(index) {
      this.dynamicTags.splice(index, 1)
    }
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 580px;
  height: 400px;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.resruitment-title {
  padding: 5px 0;
  border-bottom: 2px solid #abd3f3;
  margin-bottom: 20px;
}

.el-radio {
  margin-right: 20px;
}

.jobTime {
  display: flex;
  align-items: center;
}

.jobTime span {
  padding: 0 10px;
}
</style>


